package com.shop.loan.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shop.loan.mapper.GoodsByMybatisMapper;
import com.shop.loan.mapper.GoodsSizeByMybatisMapper;
import com.shop.loan.model.Channel;
import com.shop.loan.model.User;
import com.shop.loan.model.common.Code;
import com.shop.loan.model.common.ResResult;
import com.shop.loan.model.entity.GoodsByMybatis;
import com.shop.loan.model.vo.GoodsVo;
import com.shop.loan.service.ChannelService;
import com.shop.loan.service.ClassifyService;
import com.shop.loan.service.GoodsService;
import com.shop.loan.util.PageGoodsUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import xyz.erupt.jpa.dao.EruptDao;

import javax.annotation.Resource;
import java.util.*;

@Slf4j
@Service
public class GoodsServiceImpl implements GoodsService {

    @Resource
    EruptDao eruptDao;

    @Resource
    ClassifyService classifyService;

    @Resource
    GoodsByMybatisMapper goodsByMybatisMapper;

    @Lazy
    @Resource
    ChannelService channelService;

    @Resource
    GoodsSizeByMybatisMapper goodsSizeByMybatisMapper;

    @Override
    public PageGoodsUtil list(GoodsVo goodsVo) {
        GoodsByMybatis goodsByMybatis = new GoodsByMybatis(goodsVo);
        goodsByMybatis.setIsShow(true);

        if (goodsVo.getChannelShow() != null) {
            goodsByMybatis.setChannelShow(goodsVo.getChannelShow());
        } else {
            goodsByMybatis.setChannelShow(false);
            if (goodsVo.getOpId() != null && goodsVo.getOpId() > 0) {
                User rentalUser = eruptDao.queryEntity(User.class, "id = :id", new HashMap<String, Object>(1) {{
                    this.put("id", goodsVo.getOpId());
                }});
                if (rentalUser != null && rentalUser.getChannel() != null && rentalUser.getChannel().getId() > 1) {
                    goodsByMybatis.setChannelShow(true);
                }
            } else if (org.springframework.util.StringUtils.hasLength(goodsVo.getChannelCode())) {
                Channel channel = channelService.getChannelByCode(goodsVo.getChannelCode());
                if (channel != null && channel.getId() > 0) {
                    goodsByMybatis.setChannelShow(true);
                }
            }
        }

        if (goodsVo.getClassifyId() != null) {
            goodsByMybatis.setClassifyIdStr(StringUtils.join(classifyService.getClassifyDown(goodsVo.getClassifyId()), ","));
        }
        PageHelper.startPage(goodsVo.getPageNum(), goodsVo.getPageSize());
        Page<GoodsByMybatis> goodsByMybatisPage = goodsByMybatisMapper.selectByList(goodsByMybatis);

        for (GoodsByMybatis byMybatis : goodsByMybatisPage) {
            byMybatis.setMinMoney(goodsSizeByMybatisMapper.selectByGoodsId(byMybatis.getId()));
        }

        //封装的分页
        PageGoodsUtil page = new PageGoodsUtil();
        page.setPageSize(goodsVo.getPageSize());
        page.setTotal(goodsByMybatisPage.getTotal());
        page.setPageIndex(goodsByMybatisPage.getPages());

        page.setDataList(goodsByMybatisPage);
        return page;
    }

    @Override
    public ResResult detail(GoodsVo goods) {
        if (goods.getId() == null || goods.getId() == 0) {
            return new ResResult(Code.PARAM_LACK);
        }
        GoodsByMybatis goodsByMybatis = goodsByMybatisMapper.selectByPrimaryKey(goods.getId());
        if (goodsByMybatis == null || goodsByMybatis.getIsDelete()) {
            return new ResResult(Code.PARAM_INVALID);
        }
        goodsByMybatis.setMinMoney(goodsSizeByMybatisMapper.selectByGoodsId(goodsByMybatis.getId()));
        goodsByMybatis.setGoodsSizes(goodsSizeByMybatisMapper.selectByList(goodsByMybatis.getId()));
        return new ResResult(Code.SUCCESS, goodsByMybatis);
    }
}
